package frontend_package;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JPanel;

import backend_package.Login;
import backend_package.Packet;

/**
 * The main frame class in the view for the design. The frame handles the
 * graphical user interface, including the panels and interaction.
 * 
 * @author Kyler [minor]
 * 
 */
public class MainFrame extends Observable implements Observer {
	/** MAin frame */
	private JFrame mainFrame = null;
	/** Screen dimensions */
	private Dimension screenDimension = null;
	/** Login panel */
	private LoginPanel loginpanel;
	/** Analyst panel */
	private AnalystPanel analystpanel;
	/** Admin panel */
	private AdminPanel adminpanel;
	/** Header Panel */
	private HeaderPanel headerPanel;
	/** Content Panel */
	private JPanel contentPanel;
	/** Add question panel */
	private AddQuestionsPanel addqpanel;

	/**
	 * The main frame instantiates all the panels needed to function properly
	 * along with creating the window and relative size based on the size of the
	 * screen.
	 * 
	 * @author
	 */
	public MainFrame() {

		addqpanel = new AddQuestionsPanel();
		mainFrame = new JFrame("Analyst Application");
			
		URL url = MainFrame.class.getResource("/10.png");
		Image	image = Toolkit.getDefaultToolkit().getImage(url);

		mainFrame.setIconImage(image);

		screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
		int screenWidth = (int) screenDimension.getWidth();
		int screenHeight = (int) screenDimension.getHeight();
		Dimension frameDimension = new Dimension(1000, 650);
		mainFrame.setPreferredSize(frameDimension);
		mainFrame.setLayout(new BorderLayout());

		loginpanel = new LoginPanel();
		adminpanel = new AdminPanel();
		analystpanel = new AnalystPanel();

		headerPanel = new HeaderPanel();

		contentPanel = loginpanel.getPanel();

		// panels go here
		mainFrame.add(headerPanel.getPanel(), BorderLayout.NORTH);
		mainFrame.add(contentPanel, BorderLayout.CENTER);
		mainFrame.pack();
		mainFrame.setLocation((int) (screenWidth * 0.3) / 2,
				(int) (screenHeight * 0.1) / 2);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	/**
	 * Sets visible boolean in frame
	 * 
	 * @author Kylar
	 * @param flag
	 */
	public void setVisible(boolean flag) {
		mainFrame.setVisible(flag);
	}

	/**
	 * Sets the active panel on the window.
	 * 
	 * @author Kyler
	 * @param panel
	 */
	private void setActivePanel(JPanel panel) {
		if (panel != contentPanel) {
			mainFrame.remove(contentPanel);
			mainFrame.add(panel, BorderLayout.CENTER);
			this.contentPanel = panel;
			mainFrame.revalidate();
			mainFrame.repaint();
			mainFrame.pack();
		}
	}

	/**
	 * Gets analyst Panel
	 * 
	 * @author Kylar
	 * @return analystPanel
	 */
	public AnalystPanel getAnalystPanel() {
		return analystpanel;
	}

	/**
	 * gets AdminPanel
	 * 
	 * @author Kylar
	 * @return adminPAnel
	 */
	public AdminPanel getAdminPanel() {
		return adminpanel;
	}

	/**
	 * Gets loginPanel
	 * 
	 * @author Kylar
	 * @return loginPanel
	 */
	public LoginPanel getLogPanel() {
		return loginpanel;
	}

	/**
	 * Returns addqpanel
	 * 
	 * @author Kylar
	 * @return addpanel
	 */
	public AddQuestionsPanel getQpanel() {
		return addqpanel;
	}

	/**
	 * Gets headerPanel
	 * 
	 * @author Kyler
	 * @return
	 */
	public HeaderPanel getHeaderPanel() {
		return headerPanel;
	}

	/**
	 * Exits the frame
	 * 
	 * @author Kylar
	 */
	public void exitFrame() {
		mainFrame.dispose();
	}

	/**
	 * Open the question panel
	 * 
	 * @author Syrym
	 */
	@SuppressWarnings("static-access")
	private void openQpanel() {
		JFrame qFrame = new JFrame();
		Dimension qFrameDimension = new Dimension(1000, 650);
		qFrame.setPreferredSize(qFrameDimension);
		// panels go here
		qFrame.add(addqpanel);

		qFrame.pack();
		qFrame.setLocation(mainFrame.getLocation());
		qFrame.setDefaultCloseOperation(qFrame.DISPOSE_ON_CLOSE);
		qFrame.setVisible(true);
	}

	/**
	 * The main frame manages the various panels on the GUI. The frame handles
	 * the events of logging in and out, what the user requires, and so on.
	 * 
	 * @author Kyler
	 * @author Syrym
	 */
	@Override
	public void update(Observable o, Object arg) {
		Packet packet = (Packet) arg;
		if (o instanceof Login && packet.getTag().equals("l_analystchosen_m")) {
			setActivePanel(this.analystpanel.getPanel());
		} else if (o instanceof Login
				&& packet.getTag().equals("ad_adminchosen_m")) {
			setActivePanel(this.adminpanel.getPanel());
		} else if (o instanceof Listeners
				&& packet.getTag().equals("ap_logoutuserpanel")) { 
			// Handle AnalystPanel exiting.
			setActivePanel(this.loginpanel.getPanel());
		} else if (o instanceof AdminPanel
				&& packet.getTag().equals("ad_logoutuserpanel")) {
			// Handle AnalystPanel exiting.
			setActivePanel(this.loginpanel.getPanel());
		} else if (o instanceof LoginPanel
				&& packet.getTag().equals("lp_exitframe")) {
			exitFrame();
		} else if (o instanceof AnalystPanel
				&& packet.getTag().equals("ap_openqpanel_m")) {
			openQpanel();
		} else if (o instanceof HeaderPanel
				&& packet.getTag().equals("hp_logoutuserpanel")) {
			setActivePanel(this.loginpanel.getPanel());
		}
	}
}
